package net.gianrishandy.jbase.connection;

import java.sql.ResultSet;

import net.gianrishandy.jbase.model.DataTable;

/**
 * @author Gian Rishandy
 */
public class Command {
	
	/**
	 * Value of query
	 */
	private String query;
	
	/**
	 * Value of store procedure
	 */
	private String storeProcedure;
	
	/**
	 * Value of parameter name from store procedure
	 */
	private String[] parameterName;
	
	/**
	 * Value of parameter value from store procedure
	 */
	private Object[] parameterValue;
	
	/**
	 * Get value of SQL script from command 
	 * 
	 * @return SQL Script
	 */
	public String getSQLScriptInCommand(){
		String script = "";
		
		if(query != null){
			script = query;
		}else{
			switch(Connection.connectionType){
				case Connection.MySQL :
					script = "Call " + storeProcedure + "(";
					
					if(parameterValue != null){
						for(int i=0;i<parameterValue.length;i++){
							if(i == 0){
								if(parameterValue[i] == null){
									script = script + null;
								}else{
									script = script + "'" + parameterValue[i] + "'";
								}
							}else{
								if(parameterValue[i] == null){
									script = script + ", " + null;
								}else{
									script = script + ", '" + parameterValue[i] + "'";
								}
							}
						}
					}
					
					script = script + ")";
					break;
				case Connection.SQLServer :
					script = "exec " + storeProcedure + " ";
					
					if(parameterValue != null && parameterName != null){
						for(int i=0;i<parameterValue.length;i++){
							if(i == 0){
								script = script + parameterName[i] + "='" + parameterValue[i] + "'";
							}else{
								script = script + ", " + parameterName[i] + "='" + parameterValue[i] + "'";
							}
						}
					}
					break;
			}
		}
		
		return script;
	}
	
	/**
	 * Execution query into database
	 * 
	 * @return ResultSet
	 * @throws Exception
	 */
	public ResultSet executeQuery() throws Exception{
		return SQLExecution.executeQuery(getSQLScriptInCommand());
	}
	
	/**
	 * Execution update into database
	 * 
	 * @return boolean, true = success and false = failed
	 * @throws Exception
	 */
	public boolean executeUpdate() throws Exception{
		return SQLExecution.executeUpdate(getSQLScriptInCommand());
	}
	
	
	/**
	 * Execute query into database
	 * 
	 * @return DataTable
	 * @throws Exception
	 */
	public DataTable executeDatatable() throws Exception{
		DataTable datatable = new DataTable();
		datatable.setDatatable(executeQuery());
		return datatable;
	}
	
	public String getQuery() {
		return query;
	}
	public void setQuery(String query) {
		this.query = query;
	}
	public String getStoreProcedure() {
		return storeProcedure;
	}
	public void setStoreProcedure(String storeProcedure) {
		this.storeProcedure = storeProcedure;
	}
	public String[] getParameterName() {
		return parameterName;
	}
	public void setParameterName(String[] parameterName) {
		this.parameterName = parameterName;
	}
	public Object[] getParameterValue() {
		return parameterValue;
	}
	public void setParameterValue(Object[] parameterValue) {
		this.parameterValue = parameterValue;
	}
}
